class MapManager {




    private maps: { [index: string]: GameMap } = {};
    private container: renderer.DisplayObjectContainer;
    private currentMap = "map"
    parseFromConfig(id: string, obj: any) {
        this.maps[id] = new GameMap(obj.map[0]);
    }

    initContainer(container: renderer.DisplayObjectContainer) {
        this.container = container;
    }

    changeMap(mapId: string) {
        this.currentMap = mapId;
        const map = this.getMap();
        this.container.removeAllChildren();
        this.container.addChild(map);

        const mapStartNode = { x: 0, y: 0 };
        map.grid.setStartNode(mapStartNode.x, mapStartNode.y);
        //map.addToRoleContainer(user.view)
        //user.view.x = mapStartNode.x * TILE_SIZE;
        //user.view.y = mapStartNode.y * TILE_SIZE;

        map.addEventListener("click", (eventData: any) => {
            console.log(222)
            const x = eventData.localX;
            const y = eventData.localY;
            const row = Math.floor(x / TILE_SIZE);
            const col = Math.floor(y / TILE_SIZE);


            const info = map.getTileNodeInfo(row, col);

            pool.clear();
            const walk = new WalkCommand(row, col);
            pool.add(walk);


            const npc = map.getNpcInfo(row, col);
            if (npc) {
                const talk = new TalkCommand(npc);
                pool.add(talk)
                const gotoNextMap = new GotoNextMapCommand("map1");
                pool.add(gotoNextMap)
            }

            const item = map.getItemInfo(row, col);

            if (item) {
                const pick = new PickCommand(item);
                pool.add(pick)
            }


            pool.execute();

        })
    }

    getMap() {
        return this.maps[this.currentMap]
    }
}



const RIVER = 0;
const GRESS = 1;
const TILE_SIZE = 64;




class GameMap extends renderer.DisplayObjectContainer {

    grid: astar.Grid
    private config: any;
    private itemConfig: { [index: string]: Item } = {};
    private npcConfig: { [index: string]: Npc } = {};

    private tileContainer = new renderer.DisplayObjectContainer();
    private itemContainer = new renderer.DisplayObjectContainer();
    private roleContainer = new renderer.DisplayObjectContainer();

    constructor(config: any) {
        super();
        this.config = config;
        this.grid = new astar.Grid(8, 8);
        this.addChild(this.tileContainer);
        this.addChild(this.itemContainer);
        this.addChild(this.roleContainer);



        for (let item of this.config) {

            const texture = new Image();
            switch (item.tile) {
                case RIVER:
                    texture.src = "tile1.png";
                    break;
                case GRESS:
                    texture.src = "tile2.png"
                    break;
                default:
                    alert("错误的地图配置!!" + JSON.stringify(item))
                    break;
            }
            const tile = new renderer.Bitmap(TILE_SIZE * item.x, TILE_SIZE * item.y, texture);
            const walkable = item.tile == RIVER ? false : true;
            this.grid.setWalkable(item.x, item.y, walkable);
            this.tileContainer.addChild(tile)

            if (item.item) {

                const swordTexture = new Image();
                swordTexture.src = 'sword.png';

                const weaponView = new renderer.Bitmap(0, 0, swordTexture);
                weaponView.x = TILE_SIZE * item.x;
                weaponView.y = TILE_SIZE * item.y;
                const weaponItem = new Item();
                weaponItem.name = "木剑";
                weaponItem.attack = 25;
                weaponItem.x = item.x;
                weaponItem.y = item.y;
                const key = item.x + "_" + item.y;
                this.itemConfig[key] = weaponItem;
                weaponItem.view = weaponView;
                this.itemContainer.addChild(weaponView)
            }
            if (item.npc) {
                const npc = npcManager.getNpcById(item.npc);
                this.roleContainer.addChild(npc.view);
                npc.x = item.x;
                npc.y = item.y;
                npc.view.x = npc.x * TILE_SIZE;
                npc.view.y = npc.y * TILE_SIZE;
                const key = npc.x + "_" + npc.y;
                this.npcConfig[key] = npc;
            }

        }



    }

    addToRoleContainer(child: renderer.DisplayObject) {
        if (child.parent) {
            child.parent.removeChild(child);
        }
        this.roleContainer.addChild(child);
    }

    getItemInfo(x: number, y: number) {
        return this.itemConfig[x + "_" + y];
    }

    getNpcInfo(x: number, y: number) {
        const key = x + "_" + y;
        return this.npcConfig[key]
    }

    removeItem(item: Item) {
        const key = item.x + "_" + item.y;
        delete this.itemConfig[key];
        this.removeChild(item.view);
    }

    getTileNodeInfo(x: number, y: number) {
        for (let item of this.config) {
            if (item.x == x && item.y == y) {
                return item;
            }
        }
        return null;
    }

    //在地图上设置npc
    setNpc(npcManager: NpcManager) {
        for (let npc of npcManager.npcs) {
            this.roleContainer.addChild(npc.view);
            npc.view.x = npc.x * TILE_SIZE;
            npc.view.y = npc.y * TILE_SIZE;
            const key = npc.x + "_" + npc.y;
            this.npcConfig[key] = npc;
        }
    }
}



const mapManager = new MapManager();